<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2019/7/22
 * Time: 17:23
 */

namespace app\admin\controller;


use think\Db;
use think\Request;

class Staffjiao extends Base
{
    /**
     * 首页展示
     * @return mixed|\think\response\Json
     */
    public function index(){
        if(request()->isAjax()){
            $user_name = input('user_name');
            $staff_status = input('staff_status');
            $sc_name = input('sc_name');
            $staff_soldier_type = input('staff_soldier_type');
            $where = [];
            $where['staff_type'] = 1; // 创建社团申请
            //用户名称
            if(isset($user_name) && $user_name != ''){
                $where['staff_name'] = ['like', '%' . $user_name . '%'];
            }
            //审核状态
            if(isset($staff_status) && $staff_status != ''){
                $where['staff_status'] = $staff_status;
            }
            //学校名称
            if(isset($sc_name) && $sc_name != ''){
                $where['sc_name'] = ['like', '%' . $sc_name . '%'];
            }
            //类型
            if(isset($staff_soldier_type) && $staff_soldier_type != ''){
                $where['staff_soldier_type'] = $staff_soldier_type;
            }
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            // 获取总条数
            $count = Db::table('up_staff')
                ->join('up_u_user', 'user_id = staff_user_id', 'left')
                ->join('up_school', 'sc_id = staff_school_id', 'left')
                ->where($where)
                ->count();
            $lists = Db::table('up_staff')
                ->join('up_u_user', 'user_id = staff_user_id', 'left')
                ->join('up_school', 'sc_id = staff_school_id', 'left')
                ->field('staff_id, staff_status, staff_name, sc_name, staff_insert_time, staff_soldier_type,staff_user_id,user_phone')
                ->where($where)
                ->page($Nowpage,$limits)
                ->order('staff_insert_time desc')
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }
    
    /**
     * 申请详情
     * @return mixed
     */
    public function staffDetail(){
        $staff_id = input('staff_id');
        $detail = Db::table('up_staff')
            ->join('__U_USER__', 'staff_user_id = user_id', 'left')
            ->join('__SCHOOL__', 'sc_id = staff_school_id', 'left')
            ->join('__POLITICS__', 'politics_id = staff_politics', 'left')
            ->field('staff_soldier_type, staff_insert_time, staff_status, sc_name, staff_assn_name, 
            staff_assn_create_time, staff_assn_address, staff_assn_up, staff_assn_teacher, staff_assn_logo, 
            staff_assn_intro, staff_retire_people, staff_other_people, staff_activity, staff_name, 
            staff_gender, politics_name, staff_birthday, staff_faculty, staff_old_troops, staff_grade, 
            staff_specilay, staff_assn_duty, staff_phone, staff_student_card, staff_user_card, staff_sign, staff_sign_time, staff_instr_code, staff_member_code, 
            staff_reason,user_phone')
            ->where('staff_id', $staff_id)
            ->find();
        if($detail['staff_soldier_type'] == 1){
            $activity = Db::table('up_assn_activity')
                ->where('activity_id', 'IN', $detail['staff_activity'])
                ->column('activity_name');
            $detail['staff_activity'] = implode(',', $activity);
        }
        $this->assign([
            'detail' => $detail
        ]);
        return $this->fetch('staffjiao/staffdetail');
    }
    
    /**
     * 审核通过
     * @return \think\response\Json
     */
    public function staffAllow(){
        $staff_id = input('id');
        // 判断同类型，同学校，有没有审核通过的记录
        $staff_info = Db::table('up_staff')->where('staff_id', $staff_id)->find();
        if($staff_info['staff_soldier_type'] == 1){
            $is_have = Db::table('up_staff')
                ->where('staff_type', $staff_info['staff_type'])
                ->where('staff_status', 2)
                ->where('staff_id', 'NEQ', $staff_id)
                ->where('staff_school_id', $staff_info['staff_school_id'])
                ->where('staff_soldier_type', 1)
                ->find();
            if($is_have){
                return json(['code' => 100, 'msg' => '已有审核成功的同类型同学校记录']);
            }
        }
        
        Db::startTrans();
        try{
            $staff_instr_code = '';
            //============== 审核通过，军事类，需要计算教员代码
            if($staff_info['staff_soldier_type'] == 1){
                //============== 计算教员代码  学校代码+年月各两位+B
                // 学校代码
                $school_info = Db::table('up_staff')
                    ->where('staff_id', $staff_id)
                    ->field('staff_school_id')
                    ->find();
                $school_code = Db::table('up_school')
                    ->where('sc_id', $school_info['staff_school_id'])
                    ->value('sc_code');
    
                // 年+月
                $date = date('Ym');
                $date_str = substr($date, 2, 4);
    
                $staff_instr_code = $school_code . $date_str . 'B';
            }
            
            // 更新认证表
            $update = [
                'staff_status' => 2,
                'staff_instr_code' => $staff_instr_code
            ];
            Db::table('up_staff')->where('staff_id', $staff_id)->update($update);
            Db::commit();
            $res = 1;
        }catch(\Exception $e){
            Db::rollback();
            $res = 2;
        }
        
        if($res == 1){
            //TODO:推送
//            $user_id = Db::table('up_staff')->where('staff_id', $staff_id)->value('staff_user_id');
//            $list = [];//需要推送的用户数组
//            $send_user_id = (string)$user_id;
//            array_push($list, $send_user_id);
//            try{
//                $this->Jpush(1, $list, '您提交的认证通过审核啦', ['role_user_id' => $user_id, 'status' => 1, 'send_type' => 13]);
//            }catch(\Exception $e){
//            }
            return json(['code' => 200, 'msg' => '审核成功']);
        }else{
            return json(['code' => 100, 'msg' => '审核失败']);
        }
    }
    
    /**
     * 审核驳回
     * @return mixed|\think\response\Json
     */
    public function staffRefuse(){
        if(Request::instance()->isPost()){
            $id = input('staff_id');
            $staff_reason = input('staff_reason');
//            $length = mb_strlen($staff_status, 'UTF8');
//            if($length > 30){
//                return json(['code' => 100, 'msg' => '长度不可超过30个字']);
//            }
            $update = [
                'staff_status' => 3,
                'staff_reason' => $staff_reason,
            ];
            $res = Db::table('up_staff')->where('staff_id', $id)->update($update);
            if($res){
//                //TODO:推送
//                $user_id = Db::table('up_staff')->where('staff_id', $id)->value('staff_user_id');
//                $list = [];//需要推送的用户数组
//                $send_user_id = (string)$user_id;
//                array_push($list, $send_user_id);
//                try{
//                    $this->Jpush(1, $list, '您提交的认证未通过审核', ['role_user_id' => $user_id, 'status' => 2, 'send_type' => 13]);
//                }catch(\Exception $e){
//                }
                return json(['code' => 200, 'msg' => '审核成功']);
            }else{
                return json(['code' => 100, 'msg' => '审核失败']);
            }
        }
        $staff_id = input('staff_id');
        $this->assign('staff_id', $staff_id);
        return $this->fetch('staffjiao/staffrefuse');
    }
    
    /**
     * 删除提交
     * @return \think\response\Json
     */
    public function staffDel(){
        $staff_id = input('id');
        $res = Db::table('up_staff')->where('staff_id', $staff_id)->setField('staff_user_id','');
        if($res){
            return json(['code' => 200, 'msg' => '删除成功']);
        }else{
            return json(['code' => 100, 'msg' => '删除失败']);
        }
    }
}